package com.ttc.mBackup.BackupContacts;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



public class JsonMaker {
	ContactBackup contactBakup;

	public JsonMaker() {
		super();
	}

	
	public String toJSONString(ContactBackup contactBackup) {
		String jString = "";
		jString = createContactBackupJObj(contactBackup)
				.toString();
		return jString;
	}

	private JSONObject createContactBackupJObj(ContactBackup contactBackup) {
		JSONObject contactBackupJObj = new JSONObject();
		try {
			contactBackupJObj.put("backupTime", contactBackup.getBackupTime());
			contactBackupJObj.put("note", contactBackup.getNote());
			contactBackupJObj.put("contacts", createContactJArray(contactBackup
					.getContacts()));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return contactBackupJObj;
	}

	private JSONObject createContactJObj(Contact contact) {
		JSONObject contactJObj = new JSONObject();
		try {
			contactJObj.put("structuredName",
					createStructuredNameObj(contact.getStructuredName()));
			contactJObj.put("addresses", createAddressJArray(contact
					.getAddresses()));
			contactJObj.put("emails", createEmailJArray(contact.getEmails()));
			contactJObj.put("ims", createIMJArray(contact.getIms()));
			contactJObj.put("organizations", createOrganizationJArray(contact
					.getOrganizations()));
			contactJObj.put("phones", createPhoneJArray(contact.getPhones()));
			
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return contactJObj;
	}

	private JSONArray createContactJArray(ArrayList<Contact> contactList) {
		JSONArray contactJArray = new JSONArray();
		for (Contact contact : contactList) {
			contactJArray.put(createContactJObj(contact));
		}
		return contactJArray;
	}

	private JSONObject createAddressJObj(Address address) {
		JSONObject addressJObj = new JSONObject();
		try {
			addressJObj.put("street", address.getStreet());
			addressJObj.put("city", address.getCity());
			addressJObj.put("poBox", address.getPoBox());
			addressJObj.put("neighborhood", address.getNeighborhood());
			addressJObj.put("state", address.getState());
			addressJObj.put("zipCode", address.getZipCode());
			addressJObj.put("country", address.getCountry());
			addressJObj.put("type", address.getType());
		} catch (JSONException e) {

			e.printStackTrace();
		}
		return addressJObj;
	}

	private JSONArray createAddressJArray(ArrayList<Address> addressList) {
		JSONArray addressJArray = new JSONArray();
		for (Address add : addressList) {
			addressJArray.put(createAddressJObj(add));
		}
		return addressJArray;
	}

	private JSONObject createPhoneJObj(Phone phone) {
		JSONObject phoneJObj = new JSONObject();
		try {
			phoneJObj.put("number", phone.getNumber());
			phoneJObj.put("type", phone.getType());
		} catch (JSONException e) {

			e.printStackTrace();
		}
		return phoneJObj;
	}

	private JSONArray createPhoneJArray(ArrayList<Phone> phoneList) {
		JSONArray phoneJArray = new JSONArray();
		for (Phone phone : phoneList) {
			phoneJArray.put(createPhoneJObj(phone));
		}
		return phoneJArray;
	}

	private JSONObject createEmailJObj(Email email) {
		JSONObject emailJObj = new JSONObject();
		try {
			emailJObj.put("email", email.getEmail());
			emailJObj.put("type", email.getType());

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return emailJObj;
	}

	private JSONArray createEmailJArray(ArrayList<Email> emailList) {
		JSONArray emailJArray = new JSONArray();
		for (Email email : emailList) {
			emailJArray.put(createEmailJObj(email));
		}
		return emailJArray;
	}

	private JSONObject createIMJObj(IM im) {
		JSONObject imJObj = new JSONObject();
		try {
			imJObj.put("im", im.getIm());
			imJObj.put("type", im.getType());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return imJObj;
	}

	private JSONArray createIMJArray(ArrayList<IM> imList) {
		JSONArray imJArray = new JSONArray();
		for (IM im : imList) {
			imJArray.put(createIMJObj(im));
		}
		return imJArray;
	}

	private JSONObject createOrganizationJObj(Organization org) {
		JSONObject orgJObj = new JSONObject();
		try {
			orgJObj.put("company", org.getCompany());
			orgJObj.put("title", org.gettitle());
			orgJObj.put("type", org.getType());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return orgJObj;
	}

	private JSONArray createOrganizationJArray(ArrayList<Organization> orgList) {
		JSONArray orgJArray = new JSONArray();
		for (Organization org : orgList) {
			orgJArray.put(createOrganizationJObj(org));
		}
		return orgJArray;
	}

	private JSONObject createStructuredNameObj(StructuredName name) {
		JSONObject strnameJObj = new JSONObject();
		try {
			strnameJObj.put("displayName", name.getDisplayName());
			strnameJObj.put("familyName", name.getFamilyName());
			strnameJObj.put("givenName", name.getGivenName());
			strnameJObj.put("middleName", name.getMiddleName());
			strnameJObj.put("namePrefix", name.getNamePrefix());
			strnameJObj.put("nameSuffix", name.getNameSuffix());
			strnameJObj.put("phoneticFamilyName", name.getPhoneticFamilyName());
			strnameJObj.put("phoneticGivenName", name.getPhoneticGivenName());
			strnameJObj.put("phoneticMiddleName", name.getPhoneticMiddleName());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return strnameJObj;

	}

}
